﻿using System;
using System.Collections.Generic;
using System.Text;
//new 
using System.Reflection;
using System.Threading;
using log4net;
using log4net.Core;
using log4net.Config;
using log4net.Appender;
using System.IO;

namespace DataRecv.tools
{
    public class LogTool
    {
        /// <summary>
        /// 调试日志开关
        /// </summary>
        public static bool gDebugEnable = false;
        /// <summary>
        /// 业务流程日志开关
        /// </summary>
        public static bool gWorkEnable = true;
        public static readonly ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static bool logWatching = true;
        private static MemoryAppender logger;
        private static Thread logWatcher;
        public delegate void LogEvent(string msg);
        public event LogEvent LogComingEvent;
        private Timer deletetiemr;
        public LogTool()
        {
            try
            {
                logger = new MemoryAppender();
                BasicConfigurator.Configure(logger);
                logWatcher = new Thread(new ThreadStart(LogWatcher));
                logWatcher.Start();
                deletetiemr = new Timer(new TimerCallback(DeleteOldLog), null, TimeSpan.FromSeconds(0), TimeSpan.FromDays(1));
            }
            catch { }
        }

        private void DeleteOldLog(object obj)
        {
            DeleteLogFile(AppDomain.CurrentDomain.BaseDirectory + "Log", 10);
        }

        private void LogWatcher()
        {
            while (logWatching)
            {
                Thread.Sleep(200);
                try
                {
                    LoggingEvent[] events = logger.GetEvents();
                    if (events != null && events.Length > 0)
                    {
                        lock (events)
                        {
                            logger.Clear();
                            foreach (LoggingEvent ev in events)
                            {
                                if (LogComingEvent != null)
                                {
                                    LogComingEvent(ev.RenderedMessage);
                                }
                            }
                        }
                    }
                }
                catch { }
            }
        }

        public void Dispose()
        {
            logWatching = false;

            try
            {
                if (logWatcher != null) logWatcher.Join();
            }
            catch { }

            try
            {
                deletetiemr.Dispose();
            }
            catch { }

            try
            {
                if (logger != null) logger.Close();
            }
            catch { }

        }

        private void DeleteLogFile(string logFolder, int saveDays)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(logFolder.Trim());
                if (di.Exists)
                {
                    FileInfo[] info = di.GetFiles("*.log", SearchOption.TopDirectoryOnly);
                    if (info != null)
                    {
                        foreach (FileInfo fi in info)
                        {
                            try
                            {
                                TimeSpan span = DateTime.Now - fi.LastWriteTime;
                                if (span.TotalDays > saveDays)
                                {
                                    fi.Delete();
                                }
                            }
                            catch { }
                        }
                    }
                }
            }
            catch { }
        }
    }
}
